package com.jsjosem.aftersales.ui.notifications;

import static com.jsjosem.aftersales.utils.CommonUtils.showBigImg;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.jsjosem.aftersales.R;
import com.jsjosem.aftersales.adapter.ImgListViewAdapter;
import com.jsjosem.aftersales.component.CustomGridView;
import com.jsjosem.aftersales.data.response.EventReceiptResponse;
import com.jsjosem.aftersales.data.response.ImgResponse;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link EventReceiptInfoFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class EventReceiptInfoFragment extends Fragment {

    private EventReceiptResponse receipt;
    private CustomGridView imgGridView;
    private Context context;
    private TextView tvDesc;

    private EventReceiptInfoFragment(EventReceiptResponse receipt) {
        this.receipt = receipt;
    }

    public static EventReceiptInfoFragment newInstance(EventReceiptResponse receipt) {
        return new EventReceiptInfoFragment(receipt);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_event_receipt_info, container, false);
        context = view.getContext();
        tvDesc = view.findViewById(R.id.tv_description);
        imgGridView = view.findViewById(R.id.img_grid_view);
        tvDesc.setText(receipt.getDescription());
        List<ImgResponse> images = receipt.getImages();
        if (CollUtil.isNotEmpty(images)) {
            List<String> imgUrls = images.stream().map(ImgResponse::getImg_url).collect(Collectors.toList());
            ImgListViewAdapter imgListViewAdapter = new ImgListViewAdapter(context, imgUrls);
            imgGridView.setAdapter(imgListViewAdapter);
            imgGridView.setOnItemClickListener((adapterView, listView, position, id) -> {
                // 放大图片
                showBigImg(context, imgUrls, position);
            });
        }
        return view;
    }
}